<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>分类页</title>
    <link rel="stylesheet" href="./css/style.css">
</head>

<body>
    <div class="fix-title">
        <div class="back">&lt;</div>
        <input type="text" placeholder="请输入关键字">
        <div class="text">确定</div>
    </div>
    <div class="history-list">
        <h3>历史记录 <span class="del">全部删除</span></h3>
        <ul>
            <li>记录1</li>
            <li>记录2</li>
            <li>记录3</li>
        </ul>
    </div>
    <div class="good-list">
        <ul>
            <li>
                <img src="http://yun.itheima.com/Upload/Images/20220627/62b924298e658.jpg" alt="">
                <h3>商品名称</h3>
                <p>
                    <span>￥200.200</span>
                    <span>+</span>
                </p>
            </li>
            <li>
                <img src="http://yun.itheima.com/Upload/Images/20220627/62b924298e658.jpg" alt="">
                <h3>商品名称</h3>
                <p>
                    <span>￥200.200</span>
                    <span>+</span>
                </p>
            </li>
            <li>
                <img src="http://yun.itheima.com/Upload/Images/20220627/62b924298e658.jpg" alt="">
                <h3>商品名称</h3>
                <p>
                    <span>￥200.200</span>
                    <span>+</span>
                </p>
            </li>
            <li>
                <img src="http://yun.itheima.com/Upload/Images/20220627/62b924298e658.jpg" alt="">
                <h3>商品名称</h3>
                <p>
                    <span>￥200.200</span>
                    <span>+</span>
                </p>
            </li>
            <li>
                <img src="http://yun.itheima.com/Upload/Images/20220627/62b924298e658.jpg" alt="">
                <h3>商品名称</h3>
                <p>
                    <span>￥200.200</span>
                    <span>+</span>
                </p>
            </li>
            <li>
                <img src="http://yun.itheima.com/Upload/Images/20220627/62b924298e658.jpg" alt="">
                <h3>商品名称</h3>
                <p>
                    <span>￥200.200</span>
                    <span>+</span>
                </p>
            </li>
        </ul>
        <div class="empty" style="display:none;">
            <span>空空如也...</span>
        </div>
        <p class="desc">数据正在加载中...</p>
        <p class="desc">我也是有底线的...</p>
    </div>
    <script src="../lib/jquery.js"></script>
    <script>
        $(function () {
            let lis = document.querySelectorAll('.cate-list-fix li')
            let searchList = document.querySelector('.history-list ul ')
            $('.del').on('click', function () {
                searchList.innerHTML = ''
            })
            searchList.innerHTML = ''
            function fn(url) {

                $.ajax({
                    url: url,
                    method: "GET",
                    success: function ({ data }) {
                        document.querySelector('.good-list ul').innerHTML = ""
                        data.list.map((element, i) => {
                            const li = document.createElement('li')
                            li.innerHTML = `
                            <li>
                                <img src="${element.img}" alt="">
                                  <h3>${element.title}</h3>
                                    <p>
                                        <span class="price">￥${(element.price) / 100}</span>
                                        <span class="add">+</span>
                                    </p>
                             </li>
                               `
                            document.querySelector('.good-list ul').appendChild(li)
                        })

                    }
                })

            } fn(`http://124.223.14.236:3001/api/goods`)

            $('.text').on('click', function () {
                const searchText = $('input').val().trim()
                if (searchText.length > 0) {
                    const li = document.createElement('li')
                    li.innerHTML = searchText
                    searchList.appendChild(li)
                    $.ajax({
                        url: "http://124.223.14.236:3001/api/goods/cate",
                        method: "GET",
                        success: function ({ data }) {
                            data.map((element, i) => {
                                if (searchText === element.name) {
                                    console.log(i);
                                    fn(`http://124.223.14.236:3001/api/goods?cate_id=${i + 1}`)
                                }
                                else {
                                    document.querySelector('.good-list ul').innerHTML = ""
                                }
                            })
                        }
                    })
                }
            })
        })
    </script>
</body>

</html>